#include<bits/stdc++.h>
#define ll long long
#define N 200005
#define ls (x<<1)
#define rs (x<<1|1)
#define mid (l+r>>1)
using namespace std;
int n,q;
int fa[N],siz[N],son[N];
int idx,dfn[N],top[N];
int cnt,rt[N],Ls[N*18],Rs[N*18],s[N*18],p[N<<2];
ll dep[N],lst;
int ff[N][20],h[N];
vector<pair<int,int> > g[N];
void dfs1(int x,int f){
    fa[x]=f;
    siz[x]=1;
    ff[x][0]=f;
    for(int i=1;i<=17;i++){
        ff[x][i]=ff[ff[x][i-1]][i-1];
        if(!ff[x][i]) break;
        h[x]=i;
    }
    for(pair<int,int> i:g[x]){
        int v=i.first,w=i.second;
        if(v==f) continue;
        dep[v]=dep[x]+w;
        dfs1(v,x);
        siz[x]+=siz[v];
        if(siz[v]>siz[son[x]]) son[x]=v;
    }
}
void dfs2(int x,int topf){
    top[x]=topf;
    dfn[x]=++idx;
    if(!son[x]) return;
    dfs2(son[x],topf);
    for(pair<int,int> i:g[x]){
        int v=i.first,w=i.second;
        if(v==fa[x]||v==son[x]) continue;
        dfs2(v,v);
    }
}
inline int Lca(int x,int y){
    if(!x||!y) return (x|y);
    while(top[x]!=top[y]){
        if(dep[top[x]]<dep[top[y]]) swap(x,y);
        x=fa[top[x]];
    }
    if(dep[x]>dep[y]) swap(x,y);
    return x;
}
void build(int x,int l,int r){
    if(l==r){
        p[x]=l;
        return;
    }
    build(ls,l,mid);
    build(rs,mid+1,r);
    p[x]=Lca(p[ls],p[rs]);
}
int LCA(int x,int l,int r,int L,int R){
    if(L<=l&&r<=R) return p[x];
    if(R<=mid) return LCA(ls,l,mid,L,R);
    if(L>mid) return LCA(rs,mid+1,r,L,R);
    return Lca(LCA(ls,l,mid,L,R),LCA(rs,mid+1,r,L,R)); 
}
void update(int x,int &y,int l,int r,int id){
    y=++cnt;
    s[y]=s[x]+1;
    if(l==r) return;
    if(id<=mid) Rs[y]=Rs[x],update(Ls[x],Ls[y],l,mid,id);
    else Ls[y]=Ls[x],update(Rs[x],Rs[y],mid+1,r,id);
}
bool Query(int x,int y,int l,int r,int L,int R){
    if(s[x]==s[y]) return 0;
    if(L<=l&&r<=R) return (s[x]-s[y]>0);
    if(R<=mid) return Query(Ls[x],Ls[y],l,mid,L,R);
    if(L>mid) return Query(Rs[x],Rs[y],mid+1,r,L,R);
    if(Query(Ls[x],Ls[y],l,mid,L,R)) return 1;
    return Query(Rs[x],Rs[y],mid+1,r,L,R); 
}
inline ll Sum(int x,int y){
    return dep[x]+dep[y]-2*dep[Lca(x,y)];
}
inline bool check(int k,int l,int r){
    return Query(rt[r],rt[l-1],1,n,dfn[k],dfn[k]+siz[k]-1);
}
signed main(){
    freopen("memory.in","r",stdin);
    freopen("memory.out","w",stdout);
    scanf("%d %d",&n,&q);
    for(int i=1,u,v,w;i<n;i++){
        scanf("%d %d %d",&u,&v,&w);
        g[u].push_back({v,w});
        g[v].push_back({u,w});
    }
    dfs1(1,0);
    dfs2(1,1);
    for(int i=1;i<=n;i++) update(rt[i-1],rt[i],1,n,dfn[i]);
    build(1,1,n);
    while(q--){
        ll k,l,r;
        scanf("%lld %lld %lld",&k,&l,&r);
        k^=lst;l^=lst;r^=lst;
        int o=LCA(1,1,n,l,r);
        if(dfn[k]<dfn[o]||dfn[k]>=dfn[o]+siz[o]) lst=Sum(o,k);
        else if(check(k,l,r)) lst=0;
        else{
            int x=k;
            for(int i=h[k];i>=0;i--) if(dep[ff[x][i]]>dep[o]&&!check(ff[x][i],l,r)) x=ff[x][i];
            x=fa[x];
            lst=Sum(x,k);
        }
        printf("%lld\n",lst);
    }
    return 0;
}